﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;

namespace Freshome.b
{
    public class Magicomment : DbContext
    {
        public Magicomment()
            : base("Freshome")
        {
        }
        public IDbSet<Comment> Comments
        {
            get { return Set<Comment>(); }
        }
    }
    public abstract class Multint<T> where T : Intype
    {
        protected DbContext p_db = new Magicomment();
        public Multint() { }
        public virtual T GetEntity(Func<T, bool> predicate)
        {
            return p_db.Set<T>().AsNoTracking().Where(predicate).FirstOrDefault();
        }
        public virtual IEnumerable<T> GetEntities(Func<T, bool> predicate)
        {
            return p_db.Set<T>().AsNoTracking().Where(predicate);
        }
        public virtual IEnumerable<T> AllEntities()
        {
            return p_db.Set<T>().AsNoTracking();
        }
        public virtual int Create(T arg)
        {
            p_db.Entry(arg).State = System.Data.EntityState.Added;
            return p_db.SaveChanges();
        }
        public virtual int Update(T arg)
        {
            T t = p_db.Set<T>().Find(arg.Tid);
            if (t != null)
            {
                p_db.Entry(t).CurrentValues.SetValues(arg);
                return p_db.SaveChanges();
            }
            else return 0;
        }
        public virtual int Delete(int tid)
        {
            T t = p_db.Set<T>().Find(tid);
            if (t != null)
            {
                p_db.Set<T>().Remove(t);
                return p_db.SaveChanges();
            }
            return 0;
        }
    }
}
